package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.model.LoginDTO;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wux
 * @date 2023/5/11 9:16
 */
@Api(tags = "(运营端) 登录")
@RestController
@RequestMapping("/admin")
public class SysAdminLoginController {
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysMenuService menuService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public ResultEntity login(@Valid @RequestBody LoginDTO params) {
        // 生成令牌
        params.setChannel(1);
        return ResultEntity.createSuccess(sysLoginService.login(params));
    }

    @GetMapping("/getRouters")
    @ApiOperation(value = "获取菜单")
    public ResultEntity getRouters() {
        // 用户信息
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(SecurityUtils.getLoginUser().getUser().getId(), 1000);
        return ResultEntity.createSuccess(menuService.buildMenus(menus));
    }

    @GetMapping("/getInfo")
    @ApiOperation(value = "获取用户信息")
    public ResultEntity getInfo() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return ResultEntity.createSuccess(loginUser.getUser());
    }
}
